package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.SysDept;
import com.example.demo.entity.SysPeople;
import com.example.demo.mapper.DeptMapper;
import com.example.demo.mapper.PeopleMapper;
import com.example.demo.utils.JDBCUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/people")
public class PeopleController extends BaseController{
    @Resource
    PeopleMapper peopleMapper;

    @Resource
    DeptMapper deptMapper;

    @PostMapping("/add")
    public Result<?> save(@RequestBody SysPeople sysPeople) {
        SysDept sysDept = deptMapper.selectByOutId(sysPeople.getDeptId());
        sysPeople.setDeptName(sysDept.getName());
        sysPeople.setIsActive(true);
        sysPeople.setIsEffect(true);
        sysPeople.setCompanyId("4bbfcee379ec4334b594c768e1769d7b");
        sysPeople.setCompanyName("宁波国际会议中心管理有限公司");
        return Result.success(peopleMapper.insert(sysPeople));
    }

    @PostMapping("/update")
    public Result<?> update(@RequestBody SysPeople sysPeople){
        SysDept sysDept = deptMapper.selectByOutId(sysPeople.getDeptId());

        sysPeople.setDeptName(sysDept.getName());
        return Result.success(peopleMapper.updateById(sysPeople));
    }

    @DeleteMapping("/delete/{id}")
    public Result<?> delete(@PathVariable Long id){
        SysPeople sysPeople = peopleMapper.selectById(id);
        sysPeople.setIsEffect(false);
        return Result.success(peopleMapper.updateById(sysPeople));
    }

    //根据id获取人员
    @GetMapping("/get/{id}")
    public Result<?> getById(@PathVariable Long id) {
        return Result.success(peopleMapper.selectById(id));
    }

    @GetMapping("/list")
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search,
                              @RequestParam String company,
                              @RequestParam String department) {
        LambdaQueryWrapper<SysPeople> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SysPeople::getIsActive, true);
        wrapper.eq(SysPeople::getIsEffect, true);
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(SysPeople::getName, search);
        }
        if (StrUtil.isNotBlank(company)) {
            wrapper.eq(SysPeople::getCompanyName, company);
        }
        if (StrUtil.isNotBlank(department)) {
            wrapper.eq(SysPeople::getDeptId, department);
        }
        Page<SysPeople> peoplePage = peopleMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(peoplePage);
    }

    //根据部门名称，查找该部门的所有人
    @GetMapping("/search")
    public Result<?> searchByDeptName(@RequestParam String search) {
        return Result.success(peopleMapper.selectByDeptName(search));
    }

    //根据部门id，查找该部门的所有人
    @GetMapping("/dept/{id}")
    public Result<?> searchByDeptId(@PathVariable String id) {
        List<SysPeople> people = peopleMapper.selectByDeptId(id);
        return Result.success(people);
    }

//    @PostMapping("/synchronise")
//    public Result<?> synchronise(){
//        try{
//            JDBCUtil.getPeople();
//            JDBCUtil.getPeopleDepartment();
//        }catch (Exception e){
//            e.printStackTrace();
//            return Result.error("400", "同步失败");
//        }
//        return Result.success();
//    }
}
